package com.stone.framework.workflow.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import com.bstek.uflo.console.UfloServlet;
import com.bstek.uflo.env.EnvironmentProvider;
import com.stone.framework.core.context.UserContext;

@Configuration
@ImportResource("classpath:uflo-console-context.xml")
public class UfloConfig {

    // 覆盖uflo-console-configure.properties配置
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        // TODO 原理
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
    }

    @Bean
    public ServletRegistrationBean<UfloServlet> servletRegistrationBean() {
        return new ServletRegistrationBean<UfloServlet>(new UfloServlet(), "/uflo/*");
    }

    @Bean
    public UfloEnvironmentProvider ufloEnvironmentProvider() {
        return new UfloEnvironmentProvider();
    }

    @Bean("ufloPlatformTransactionManager")
    public PlatformTransactionManager
        ufloPlatformTransactionManager(LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean) {
        HibernateTransactionManager manager = new HibernateTransactionManager();
        manager.setSessionFactory(localContainerEntityManagerFactoryBean.getObject().unwrap(SessionFactory.class));
        return manager;
    }

    public static class UfloEnvironmentProvider implements EnvironmentProvider {

        @Autowired
        private LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean;

        @Autowired
        @Qualifier("ufloPlatformTransactionManager")
        private PlatformTransactionManager ufloPlatformTransactionManager;

        @Override
        public SessionFactory getSessionFactory() {
            SessionFactory sessionFactory =
                localContainerEntityManagerFactoryBean.getObject().unwrap(SessionFactory.class);
            return sessionFactory;
        }

        @Override
        public PlatformTransactionManager getPlatformTransactionManager() {
            return ufloPlatformTransactionManager;
        }

        @Override
        public String getLoginUser() {
            return UserContext.getDefaultUsername();
        }

        @Override
        public String getCategoryId() {
            return null;
        }

    }

}
